{extend name='table'}

{block name="button"}
<button data-open='{:url("add")}' data-title="{:lang('添加计划')}"   data-width="1000px" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('添加')}</button>

<a data-confirm="确定永久删除这些计划吗？" data-table-id="UserTable" data-action='{:url("remove")}' data-rule="id#{id}" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('批量删除')}</a>

{/block}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <div class="layui-tab-content">
        {include file='product/index_search'}
        <table id="UserTable" data-url="{:sysuri('index')}" data-target-search="form.form-search"></table>
    </div>
</div>
<script>
    $(function () {
        $('#UserTable').layTable({
            even: true, height: 'full',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', title: '{:lang("Id")}', align: 'center', minWidth: 100},
                {field: 'name', title: '{:lang("课程名称")}', align: 'center', minWidth: 170},
            
                {field: 'product_id', title: '{:lang("课程分类")}', minWidth: 100, align: 'center', templet: function(d){
                    return d.cate && d.cate.name ? d.cate.name : '-';
                }},
                {field: 'product_id', title: '{:lang("课程标签")}', minWidth: 100, align: 'center', templet: function(d){
                    return d.tag && d.tag.name ? d.tag.name : '-';
                }},
                {field: 'product_id', title: '{:lang("课程教师")}', minWidth: 100, align: 'center', templet: function(d){
                    return d.teacher && d.teacher.name ? d.teacher.name : '-';
                }},
                {
                    field: 'headimg', title: '{:lang("课程封面")}', width: 120, align: 'center', templet: function (d) {
                        if (!d.image_path) return '-';
                        return layui.laytpl('<div class="headimg headimg-ss shadow-inset margin-0" data-tips-image data-tips-hover data-lazy-src="{{d.image_path}}"></div>').render(d);
                    }
                },
                {field: 'description', title: '{:lang("课程简介")}', align: 'center'},
                {field: 'total_chapter', title: '{:lang("章节数")}', align: 'center', sort: true},
                {field: 'total_people', title: '{:lang("学员数")}', align: 'center', sort: true},
                {field: 'total_score', title: '{:lang("评分数")}', align: 'center', sort: true},
                {field: 'total_collect', title: '{:lang("收藏数")}', align: 'center', sort: true},
                {field: 'total_visits', title: '{:lang("访问数")}', align: 'center', sort: true},
                {field: 'total_number', title: '{:lang("库存数")}', align: 'center', sort: true},
                {field: 'total_comments', title: '{:lang("评论数")}', align: 'center', sort: true},
                {field: 'total_orders', title: '{:lang("订单数")}', align: 'center', sort: true},
                {field: 'status', title: '{:lang("状态")}', align: 'center', minWidth: 110, templet: '#StatusSwitchTpl'},
                {field: 'is_recommend', title: '{:lang("是否推荐")}', align: 'center', minWidth: 110, templet: '#RecommendSwitchTpl'},
                {field: 'is_free', title: '{:lang("是否免费")}', align: 'center', minWidth: 110, templet: '#FreeSwitchTpl'},
                {field: 'create_time', title: '{:lang("创建时间")}', align: 'center', minWidth: 170, sort: true},
                {toolbar: '#toolbar', title: '{:lang("操作面板")}', align: 'center', minWidth: 180, fixed: 'right'}
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            var data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserTable').trigger('reload');
                }); else {
                    $('#UserTable').trigger('reload')
                }
                return false;
            }, false);
        });

        layui.form.on('switch(RecommendSwitch)', function (obj) {
            var data = {id: obj.value, is_recommend: obj.elem.checked > 0 ? 2 : 1};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserTable').trigger('reload');
                }); else {
                    $('#UserTable').trigger('reload')
                }
                return false;
            }, false);
        });

        layui.form.on('switch(FreedSwitch)', function (obj) {
            var data = {id: obj.value, is_free: obj.elem.checked > 0 ? 2 : 1};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserTable').trigger('reload');
                }); else {
                    $('#UserTable').trigger('reload')
                }
                return false;
            }, false);
        });

        
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("state")}-->
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('上架')}|{:lang('下架')}" lay-filter="StatusSwitch" {{-d.status>0?'checked':''}}>
    <!--{else}-->
    {{-d.status ? '<b class="color-green">{:lang("已上架")}</b>' : '<b class="color-red">{:lang("已下架")}</b>'}}
    <!--{/if}-->
</script>


<script type="text/html" id="RecommendSwitchTpl">
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('推荐')}|{:lang('不推荐')}" lay-filter="RecommendSwitch" {{-d.is_recommend == 2?'checked':''}}>
</script>


<script type="text/html" id="FreeSwitchTpl">
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('免费')}|{:lang('不免费')}" lay-filter="FreedSwitch" {{-d.is_free == 2 ?'checked':''}}>
</script>is_free








<script type="text/html" id="toolbar">

    <a class="layui-btn layui-btn-sm"  data-width="1000px" data-event-dbclick data-title="{:lang('编辑')}" data-open='{:url("edit")}?id={{d.id}}'>{:lang('编 辑')}</a>

    <a class="layui-btn layui-btn-sm layui-btn-danger" data-confirm="{:lang('确定要永久删除吗？')}" data-action="{:url('remove')}" data-value="id#{{d.id}}">{:lang('删 除')}</a>

</script>
{/block}
